// pages/mainOperation/editAddress/editAddress.js
//获取应用实例
const app = getApp()
import FormValidation from '../../../../utils/formValidation'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    MailAddressID: 0, //邮寄地址主键
    MailAreaAddress: '请选择收件地址',
    MailUserName: '',
    MailAddress: '',
    MailUserMobile: '',
    MailAreaID: 0,
    IsDefault: 0, //是否默认

    FrmValidate: null, //验证组件
    SourceUrl: '', //页面来源
    cityPickerIsShow: false,
    cityPickerValue: ["0", "0", "0"], //缓存选择的城市信息
    cityInitPickerValue: ["0", "0", "0"], //首次加载默认城市信息

    modalHidden: true, //模态框隐藏
    modalMsg: '确认要删除邮寄地址吗？' //模态框中的信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData(options);
    if (parseInt(this.data.MailAddressID) > 0) {
      this.getAddressDetail();
    }
    this.initValidate()
  },
  /**
   *  地址组件的切换
   */
  toggleCityPicker: function() {
    let self = this;
    self.setData({
      cityPickerIsShow: !self.data.cityPickerIsShow
    })
  },
  /**
   *  取消地址选择
   */
  onCityCacnelClick: function(e) {
    let self = this;
    self.setData({
      cityPickerIsShow: false
    });
  },
  /**
   *  地址组件选择确认
   */
  onCitySureClick: function(e) {
    let self = this;
    const cityText = e.detail.valueName[0] + "-" + e.detail.valueName[1] + "-" + e.detail.valueName[2];
    self.setData({
      cityPickerValue: e.detail.valueCode, //缓存选择的城市信息     
      MailAreaID: e.detail.valueCode[2],
      MailAreaAddress: cityText,
      cityPickerIsShow: false
    })
  },
  /**
   * 提交
   */
  formSubmit(e) {
    const data = e.detail.value
    // 传入表单数据，调用验证方法
    if (!this.FrmValidate.checkForm(data)) {
      const error = this.FrmValidate.errorList[0];
      wx.showToast({
        title: error.msg,
        icon: 'none'
      })
      return false
    }
    data.UserID = app.userInfo.UserID;
    data.IsDefault = data.IsDefault ? 1 : 0;
    this.saveMailAddress(data);
  },
  /**
   * 保存地址
   */
  saveMailAddress(data) {
    let self = this;
    let url = app.apiBaseUrl;
    if (parseInt(self.data.MailAddressID) > 0) {
      data.MailAddressID = self.data.MailAddressID;
      url += "/MailAddress/Update";
    } else {
      url += "/MailAddress/Add";
    }
    app.httpRequest({
      method: "POST",
      url: url,
      showLoading: true,
      data: data,
      success: function(res) {
        let rdata = res.data;
        if (rdata.status == 0) {
          if (parseInt(self.data.MailAddressID) <= 0) {
            data.MailAddressID = rdata.data;
          }
          if (self.data.SourceUrl == 'Order') {
            //获取上一页
            let pages = getCurrentPages();
            let prePage = pages[pages.length - 2];
            prePage.setData({
              HasDftAddress: true, //是否存在默认地址
              MailUserName: data.MailUserName, //收件名称
              MailUserMobile: data.MailUserMobile, //收件电话
              MailAreaAddress: data.MailAreaAddress, //收件省市
              MailAddress: data.MailAddress, //收件地址
              MailAddressID: data.MailAddressID //收件主键
            });
          }
          wx.navigateBack({
            delta: 1 // 返回上一级页面。
          });
        } else {
          wx.showToast({
            title: rdata.message,
            icon: "none"
          });
        }
      }
    });
  },
  /**
   * 获取详情
   */
  getAddressDetail: function() {
    let self = this;

    let url = app.apiBaseUrl + "/MailAddress/GetDetail";
    app.httpRequest({
      method: "GET",
      url: url,
      showLoading: true,
      data: {
        addressId: self.data.MailAddressID
      },
      success: function(res) {
        let rdata = res.data;
        console.log(rdata)
        if (rdata.status == 0) {
          self.setData({
            MailAddressID: rdata.data.MailAddressID, //邮寄地址主键
            MailAreaAddress: rdata.data.MailAreaAddress,
            MailUserName: rdata.data.MailUserName,
            MailAddress: rdata.data.MailAddress,
            MailUserMobile: rdata.data.MailUserMobile,
            MailAreaID: rdata.data.MailAreaID,
            IsDefault: rdata.data.IsDefault ? 1 : 0
          });
        } else {
          wx.showToast({
            title: rdata.message,
            icon: "none"
          });
        }
      }
    });
  },
  /**
   * 验证初始化
   */
  initValidate() {
    // 验证字段的规则
    const rules = {
      MailUserName: {
        required: true,
      },
      MailUserMobile: {
        required: true,
        tel: true
      },
      MailAreaID: {
        required: true,
        min: 1,
      },
      MailAddress: {
        required: true,

      }
    }

    // 验证字段的提示信息，若不传则调用默认的信息
    const messages = {
      MailUserName: {
        required: '请输入收件人姓名',
      },
      MailUserMobile: {
        required: '请输入收件人电话号码',
        tel: '请输入正确的电话号码'
      },
      MailAreaID: {
        required: '请选择收件地址',
        min: '请选择收件地址',
      },
      MailAddress: {
        required: '请输入纤细地址',
      }
    }
    // 创建实例对象
    this.FrmValidate = new FormValidation(rules, messages)
  },
  /**
   *  删除消息
   */
  deleteTap: function(e) {
    let self = this;
    self.setData({
      modalHidden: false
    })
  },
  /**
   *  模态框组件的确认按钮
   */
  modalSureListener: function(e) {
    let self = this;
    //隐藏modal
    self.setData({
      modalHidden: true
    });
    let url = app.apiBaseUrl + "/MailAddress/Delete";
    app.httpRequest({
      method: "POST",
      url: url,
      showLoading: true,
      data: {
        Id: self.data.MailAddressID
      },
      success: function(res) {
        let rdata = res.data;
        if (rdata.status == 0) {
          wx.navigateBack({
            delta: 1
          })
        } else {
          wx.showToast({
            title: rdata.message
          });
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})